package cn.gogpay.duoim.sdk.model.v3;

import cn.gogpay.duoim.sdk.constant.IMRespCode;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;

/**
 * 回调数据结构
 */
public class IMCallbackResult extends HashMap<String, Object> {

    /**
     * 状态
     */
    public static final String ERR_CODE = "errCode";

    /**
     * 信息
     */
    public static final String ERR_MSG = "errMsg";

    /**
     * 详情
     */
    public static final String ERR_DLT = "errDlt";

    /**
     * 表示业务系统的回调是否正确执行。0表示操作成功。
     */
    public static final String ACTION_CODE = "actionCode";

    public static final String NEXT_CODE = "nextCode";

    public IMCallbackResult append(String key, Object value) {
        put(key, value);
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    private static IMCallbackResult build(Integer code, String msg, String dlt, Integer action) {
        IMCallbackResult result = new IMCallbackResult();
        result.put(ERR_CODE, code);
        result.put(ERR_MSG, msg);
        result.put(ERR_DLT, dlt);
        result.put(ACTION_CODE, action);
        return result;
    }

    public static IMCallbackResult next() {
        return build(IMRespCode.ok().getCode(), IMRespCode.ok().getMessage(), "", 0)
                .append(NEXT_CODE, 0);
    }

    public static IMCallbackResult refuse(String msg, String dlt) {
        return build(IMRespCode.refuse().getCode(), msg, dlt, 1)
                .append(NEXT_CODE, 1);
    }

    public static IMCallbackResult refuse(String msg) {
        return refuse(msg, "");
    }
}
